package protocol

import (
	"context"
	"qld2devservice/proto"
	"qld2devservice/utils"

	"google.golang.org/grpc"
)

func QiCheGuoBiaoOpen(hostUrl string, userId int64, snNum, orderSn string, amount, balance int64) (timeOutFlag bool, openFlag int64, openMsg string, err error) {
	// 连接到 gRPC 服务器
	conn, err := grpc.Dial(hostUrl, grpc.WithInsecure())
	if err != nil {
		errNew := utils.WrapError(err, "QiCheGuoBiaoOpen:1,gRPC服务器连接失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, "", errNew
	}
	defer conn.Close()

	// 初始化 gRPC 客户端
	client := proto.NewQiCheGuoBiaoClient(conn)
	res := proto.QiCheGuoBiaoOpenRequest{
		UserID: userId, SnNum: snNum, OrderSn: orderSn, Amount: amount, Balance: balance,
	}
	utils.MyPrint(utils.PRINT_INFO, "QiCheGuoBiaoOpen request", res)
	resp, err := client.QiCheGuoBiaoOpen(context.Background(), &res)
	if err != nil {
		errNew := utils.WrapError(err, "QiCheGuoBiaoOpen:2,gRPC调用失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, "", errNew
	}
	utils.MyPrint(utils.PRINT_INFO, "QiCheGuoBiaoOpen response", utils.TransProtoToJson(resp))
	return resp.TimeOut, resp.OpenFlag, resp.OpenMsg, nil
}

func QiCheGuoBiaoClose(hostUrl string, userId int64, snNum string) (timeOutFlag bool, closeFlag int64, amount int64, electricity float32, duration int64, orderSn, recordAmount, closeMsg string, err error) {
	// 连接到 gRPC 服务器
	conn, err := grpc.Dial(hostUrl, grpc.WithInsecure())
	if err != nil {
		errNew := utils.WrapError(err, "QiCheGuoBiaoClose:1,gRPC服务器连接失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, 0, 0, 0, "", "", "", errNew
	}
	defer conn.Close()

	// 初始化 gRPC 客户端
	client := proto.NewQiCheGuoBiaoClient(conn)
	res := proto.QiCheGuoBiaoCloseRequest{
		UserID: userId, SnNum: snNum,
	}
	utils.MyPrint(utils.PRINT_INFO, "QiCheGuoBiaoClose request", res)
	resp, err := client.QiCheGuoBiaoClose(context.Background(), &res)
	if err != nil {
		errNew := utils.WrapError(err, "QiCheGuoBiaoClose:2,gRPC调用失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, 0, 0, 0, "", "", "", errNew
	}
	utils.MyPrint(utils.PRINT_INFO, "QiCheGuoBiaoClose response", utils.TransProtoToJson(resp))
	return resp.TimeOut, resp.CloseFlag, resp.Amount, resp.Electricity, resp.Duration, resp.OrderSn, resp.AmountRecord, resp.CloseMsg, nil
}

func QiCheGuoBiaoForceClose(hostUrl string, userId int64, snNum string) (timeOutFlag bool, closeFlag int64, amount int64, electricity float32, duration int64, orderSn, recordAmount, closeMsg string, err error) {
	// 连接到 gRPC 服务器
	conn, err := grpc.Dial(hostUrl, grpc.WithInsecure())
	if err != nil {
		errNew := utils.WrapError(err, "QiCheGuoBiaoForceClose:1,gRPC服务器连接失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, 0, 0, 0, "", "", "", errNew
	}
	defer conn.Close()

	// 初始化 gRPC 客户端
	client := proto.NewQiCheGuoBiaoForceCloseClient(conn)
	res := proto.QiCheGuoBiaoForceCloseRequest{
		UserID: userId, SnNum: snNum,
	}
	utils.MyPrint(utils.PRINT_INFO, "QiCheGuoBiaoForceClose request", res)
	resp, err := client.QiCheGuoBiaoForceClose(context.Background(), &res)
	if err != nil {
		errNew := utils.WrapError(err, "QiCheGuoBiaoForceClose:2,gRPC调用失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, 0, 0, 0, "", "", "", errNew
	}
	utils.MyPrint(utils.PRINT_INFO, "QiCheGuoBiaoForceClose response", utils.TransProtoToJson(resp))
	return resp.TimeOut, resp.CloseFlag, resp.Amount, resp.Electricity, resp.Duration, resp.OrderSn, resp.AmountRecord, resp.CloseMsg, nil
}
